import 'package:auto_size_text/auto_size_text.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:inmotion_pro/config/theme.dart';
import 'package:lx_widgets_library/lx_widgets_library.dart';

import '../../../../config/const.dart';

class DialogItemModel {
  String? title;
  String? details;

  DialogItemModel({required this.title, this.details});
}

class DialogItemMenuModel {
  String? title;
  TextStyle? textStyle; // 优先级高于color
  Color? color;

  DialogItemMenuModel({required this.title, this.textStyle, this.color});
}

///
/// 底部弹出文本列表
///
class DialogItemList extends StatelessWidget {
  final List<DialogItemModel> dataList;
  final void Function(int index)? onTap;

  const DialogItemList({Key? key, required this.dataList, required this.onTap})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      color: lxColorScheme.blockColor,
      child: ListView.builder(
        physics: const BouncingScrollPhysics(),
        itemCount: dataList.length,
        padding: const EdgeInsets.only(bottom: 20),
        shrinkWrap: true,
        itemBuilder: (BuildContext context, int index) {
          return _buildItem(dataList[index], () {
            if (onTap != null) onTap!(index);
          });
        },
      ),
    );
  }

  static Widget _buildItem(DialogItemModel item, void Function()? onTap) {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: onTap,
      child: Column(children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Container(
              margin: const EdgeInsets.only(top: 20, bottom: 20, right: 20),
              child: Text(
                item.title ?? '',
                style: Theme.of(Get.context!).listTitleStyle,
              ),
            ),
            Expanded(
                child: SizedBox(
                    child: AutoSizeText(item.details ?? '',
                        style: Theme.of(Get.context!).listSubtitleStyle,
                        minFontSize: 8.0,
                        maxLines: 3,
                        overflow: TextOverflow.ellipsis))),
          ],
        ),
        const Divider(height: 1.0),
      ]),
    );
  }
}

///
/// 底部弹出菜单
/// 添加取消按钮
///
class DialogItemMenuListView extends StatelessWidget {
  final List<DialogItemMenuModel> dataList;
  final void Function(int index)? onTap;
  final VoidCallback? onCancel;

  const DialogItemMenuListView(
      {Key? key, required this.dataList, required this.onTap, this.onCancel})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
        color: Theme.of(Get.context!).colorScheme.blockColor,
        // padding: EdgeInsets.only(bottom: 30.h),
        child: CustomScrollView(
          physics: const BouncingScrollPhysics(),
          shrinkWrap: true,
          slivers: [
            SliverToBoxAdapter(
                child: ListView.separated(
              physics: const BouncingScrollPhysics(),
              itemCount: dataList.length,
              padding: EdgeInsets.zero,
              shrinkWrap: true,
              itemBuilder: (BuildContext context, int index) {
                DialogItemMenuModel item = dataList[index];
                return _buildMenuItem(item, () {
                  if (onTap != null) {
                    onTap!(index);
                  }
                });
              },
              separatorBuilder: (BuildContext context, int index) {
                return Divider(
                  height: 0.5,
                  color: Theme.of(context).colorScheme.dialogDividerColor,
                );
              },
            )),
            // 添加取消按钮
            SliverToBoxAdapter(
                child: buildCancelItem(
                    DialogItemMenuModel(title: 'btn_cancel'.tr), onCancel)),
            SliverToBoxAdapter(child: SizedBox(height: screenBottom)),
          ],
        ));
  }

  static Widget _buildMenuItem(
      DialogItemMenuModel item, void Function()? onTap) {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: onTap,
      child: Column(children: [
        Container(
            height: rowHeight,
            alignment: Alignment.center,
            padding:
                EdgeInsets.only(left: CONTENT_MARGIN, right: CONTENT_MARGIN),
            child: AutoSizeText(item.title ?? '',
                style: item.textStyle ??
                    TextStyle(
                        fontSize: 16,
                        color: item.color ??
                            Theme.of(Get.context!).colorScheme.titleColor),
                minFontSize: 8.0,
                maxLines: 3,
                overflow: TextOverflow.ellipsis)),
        // const Divider(height: 1.0),
      ]),
    );
  }

  static Widget buildCancelItem(
      DialogItemMenuModel item, void Function()? onCancel) {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      child: Column(children: [
        Divider(
          height: 7,
          thickness: 7,
          color: Theme.of(Get.context!).colorScheme.dialogCancelDividerColor,
        ),
        Container(
          height: rowHeight - 7,
          alignment: Alignment.center,
          // color: Colors.red,
          padding: EdgeInsets.only(left: CONTENT_MARGIN, right: CONTENT_MARGIN),
          child: Text(
            item.title ?? '',
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
            style: item.textStyle ??
                TextStyle(
                    fontSize: 16,
                    color: item.color ??
                        Theme.of(Get.context!).colorScheme.titleColor),
          ),
        ),
      ]),
      onTap: () {
        if (onCancel != null) {
          onCancel();
        }
      },
    );
  }
}
